/*
 * gameDevice.cpp
 *
 *  Created on: 24.02.2012
 *      Author: Caffeine
 */

#include "CGameDevice.h"
#include "../log/Log.h"
#include <thread/IThread.h>
#include "Environment.h"

void CGameDevice::init(){

	if (getDevice()!=0){
		getDevice()->drop();
	}
#ifdef __GAME__
	runing=false;
	screenDim.Width=1024;
	screenDim.Height=768;
	DeltaTime=0;

	device=irr::createDevice(irr::video::EDT_OPENGL,screenDim, 16,false, false, false,receiver);
	irr::thread::setMainThread();
	irr::core::Environment::setDevice(device);
#endif
#ifdef __GAME_SERVER__
	device=irr::createDevice(irr::video::EDT_NULL);
#endif
	//device->setEventReceiver(&receiver);
	smgr=getDevice()->getSceneManager();
	//irr::io::I3DVFileFormat*m=new irr::io::I3DVFileFormat(device);
	//smgr->addExternalMeshLoader(m);
	//m->drop();
#ifdef __GAME__
	guienv=getDevice()->getGUIEnvironment();
	driver=getDevice()->getVideoDriver();
	driver->getMaterial2D().TextureLayer[0].BilinearFilter=true;
	driver->getMaterial2D().AntiAliasing=irr::video::EAAM_FULL_BASIC;
#endif
	getGameObject()->init();
	;
}

#ifdef __GAME__
EventReceiver*CGameDevice::getReceiver(){

	return (receiver);
}
#endif

const char*CGameDevice::GetCompilerName(){

#if (__INTEL_COMPILER)
	return ("Intel");

#elif (__ICC)
	return ("Intel");

#elif (__KCC)
	return ("Intel's KAI C++");

#elif (__MINGW32__)
	return ("MinGW");

#elif (__COMO__)
	return ("Comeau C++");

#elif (__DECC)
	return ("Compaq C/C++");

#elif (VAXC)
	return ("Compaq C/C++");

#elif (__VAXC)
	return ("Compaq C/C++");

#elif (_CRAYC)
	return ("Cray C/C++");

#elif (__CYGWIN__)
	return ("Cygwin");

#elif (__DCC__)
	return ("Diab C/C++");

#elif (__DMC__)
	return "Digital Mars");

#elif (__SC__)
	return ("Digital Mars");

#elif (__ZTC__)
	return ("Digital Mars");

#elif (__EDG__)
	return ("EDG C++ Front End");

#elif (__GNUC__)
	return ("Gnu C/C++");

#elif (__HP_cc)
	return ("HP ANSI C/aC++");

#elif (__HP_aCC)
	return ("HP ANSI C/aC++");

#elif (__xlC__)
	return ("IBM XL C/C++");

#elif (__IBMC__)
	return ("IBM XL C/C++");

#elif (__IBMCPP__)
	return ("IBM XL C/C++");

#elif (LCC)
	return ("LCC");

#elif (__HIGHC__)
	return ("MetaWare High C/C++");

#elif (sgi)
	return ("MIPSpro");

#elif (__sgi)
	return ("MIPSpro");

#elif (__MRC__)
	return ("MPW C++");

#elif (MPW_C)
	return ("MPW C++");

#elif (MPW_CPLUS)
	return ("MPW C++");

#elif (__CC_NORCROFT)
	return ("Norcroft C");

#elif (__POCC__)
	return ("Pelles C");

#elif (SASC)
	return ("SAS/C");

#elif (__SASC)
	return ("SAS/C");

#elif (__SASC__)
	return ("SAS/C");

#elif (_SCO_DS)
	return ("SCO");

#elif (__SUNPRO_C)
	return ("Sun Workshop C/C++");

#elif (__SUNPRO_CC)
	return ("Sun Workshop C/C++");

#elif (__TenDRA__)
	return ("TenDRA C/C++");

#elif (__TINYC__)
	return ("Tiny C");

#elif (__USLC__)
	return ("USL C");

#elif (__WATCOMC__)
	return ("Watcom C++");

#elif (__MWERKS__)
	return ("MetroWerks CodeWarrior");

#elif (__TURBOC__)
	return ("Borland Turbo C");

#elif (__BORLANDC__)
	return ("Borland C++");

#elif (_MSC_VER >= 1400)
	return ("Microsoft 8.0 or higher");

#elif (_MSC_VER >= 1300)
	return ("Microsoft 7");

#elif (_MSC_VER >= 1200)
	return ("Microsoft 6");

#elif (_MSC_VER)
	return ("Microsoft, but a version lower than 6");

#else
	return ("an Unknown type");
#endif
}

CGameDevice::CGameDevice(){

	smgr=0;
	device=0;
	gameObject=0;
#ifdef __GAME__
	runing=false;
	driver=0;
	receiver=new EventReceiver(this);
#endif
	;
}

CGameDevice::~CGameDevice() {
#ifdef __GAME__
	device->closeDevice();
	delete receiver;
#endif
}

bool CGameDevice::isRuning(){

#ifdef __GAME__
	return (runing);
#endif
#ifdef __GAME_SERVER__
	return (true);
#endif
}

irr::IrrlichtDevice*CGameDevice::getDevice(){

	return (device);
}

#ifdef __GAME__
/*
irr::gui::IGUIEnvironment*CGameDevice::getGuiEnv(){

	return guienv;
}
*/
irr::u32 CGameDevice::getDeltaTimeS(){

	return (DeltaTime);
}

irr::f32 CGameDevice::getDeltaTime(){

	return ((irr::f32)getDeltaTimeS()*0.001);
}

void CGameDevice::run(){

	if (getGameObject()==0){
		runing=false;
		;
	}
	runing=true;
	irr::u32 then = device->getTimer()->getTime();
	irr::u32 now=0;
	while (getDevice()->run())
		if (getDevice()->isWindowActive()){
			now=device->getTimer()->getTime();
			getGameObject()->onRender();
			DeltaTime=(now - then);
			then = now;
		}
	getDevice()->drop();
	runing=false;
	;
}
#endif
/*
irr::video::IVideoDriver*CGameDevice::getDriver(){

	return (driver);
}

irr::scene::ISceneManager*CGameDevice::getSmgr(){

	return (smgr);
}
*/
IGameObject*CGameDevice::getGameObject(){

	return (gameObject);
}

void CGameDevice::setGameObject(IGameObject*c){

	if (gameObject!=0)
		gameObject->drop();
	gameObject=c;
	if(gameObject!=0)
		gameObject->grab();
#ifdef __GAME__
	receiver->refreshGameObject();
#endif
	;
}

#ifdef __GAME__
irr::core::dimension2d<irr::u32> CGameDevice::getScreenDim(){

	return (screenDim);
}
#endif
